import {
  request,
} from '../../../utils/requestUtils';
import { userBehavior } from '../../../behaviors/userBehavior'

import {
  debounce
} from 'miniprogram-licia'
Component({
  behaviors: [userBehavior],
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    total:0,
    page: {
      pageNum: 1,
      pageSize: 10
    },
    params: {
      keywords: ''
    },
    products: []
  },

  /**
   * 组件的方法列表
   */
  methods: {
  // 详情
  detais(e){
    console.log('e', e)
    wx.navigateTo({
      url: '/pages/customer_product_details/index?id='+e.target.dataset.id,
    })
  },
  // 查询商品信息
  async getList() {
    console.log('ces')
    const result = await request({
      url: `/bus/customer/product/list?pageNum=${this.data.page.pageNum}&pageSize=${this.data.page.pageSize}`,
      method: "POST",
      data: {
        productType:2,
        params: this.data.params
      }
    })
    if(result.data.code == 200){
      var products = this.data.products.concat(result.data.rows)
      this.setData({
        products: products,
        total: result.data.total
      })
    }
  },
  // 监听是否更改了购买数量
  onCarNumChange: debounce(async function (event) {
    console.log('event', event)
    const {detail} = event
    const {id, index} = event.target.dataset
    const result = await request({
      url: `/bus/customer/cart`,
      method: "POST",
      data: {
        productId:id,
        carNum: detail,
        checked:2
      }
    })
    if(result.data.code == 200){
      this.getCartNum()
    }
  }, 500),
  async getCartNum() {
    const result = await request({
      url: `/bus/customer/cart/cartNum`,
      method: "GET"
    })
    if (result.data.code == 200) {
      this.setCarNum(result.data.data)
    }
  }
  },
  attached:function(){
    this.getList()
  },
})